<?php

namespace app\common\model\company;

use app\api\controller\Pub;
use app\common\model\power\Rolepower;
use app\common\model\user\User;
use app\common\model\customer\Subscribe;
use think\Model;

class CompanyFile extends Model
{
    // 设置当前模型对应的完整数据表名称
    protected $table = 'company_file';
    protected $pk    = 'Id';

    const STATEINT   = 11; //初始化
    const STATEOK    = 11; //没问题的
    const WAITUPLOAD = 10; //禁止上传
    const WAITDOWN   = 11; //禁止下载

    public function companyinfo()
    {
        return $this->hasOne('app\common\model\company\Company', 'CompanyId', 'CompanyId');
    }

    public function bussinfo()
    {
        return $this->hasOne('app\common\model\user\User', 'UserId', 'BussinessId');
    }

    public function typeinfo()
    {
        return $this->hasOne('app\common\model\company\CompanyFileType', 'Id', 'TypeId');
    }

     /**
     * 创建企业档案信息
     * @param  [type] $data [description]
     * @return [type]       [description]
     */
    public static function user_create($idata)
    {
        if (empty($idata['CompanyId'])) {
            return moerr('缺少企业id');
        }
        if($idata['CompanyId']=='-1'){
            if (Rolepower::ispower(206002)) {
                //管理员权限
            }else{
                return moerr('权限不足，无法查看客户文件信息','','goback');
            }
        }else{
            $sb = Company::get($idata['CompanyId']);
            if (empty($sb)) {
                return moerr('缺少企业信息');
            }
        }
        if(empty($idata['TypeId'])){
            return moerr('缺少文件分类');
        }
        $CompanyFile               = new CompanyFile();
        $CompanyFile->State = CompanyFile::STATEINT;
        $CompanyFile->CreateTime   = date('Y-m-d H:i:s', time());
        $CompanyFile->CreateUserId = Pub::userid();
        $filed                  = ['Img', 'Name', 'Note', 'TypeId', 'SubscribeTime','File','CompanyId','SubscribeTime'];
        foreach ($filed as $key => $value) {
            if (isset($idata[$value])) {
                $CompanyFile->$value = $idata[$value];
            }
        }
        $CompanyFile->save();
        return $CompanyFile->Id;
    }

    /**
     * 获取订单列表
     * @param  [type] $data    [主数据]
     * @param  string $type    [查询类型/hawa/buss]
     * @param  [type] $is_page [是否分页显示]
     * @return [type]          [description]
     */
    public static function usergetlist($data, $type = 'buss', $is_page = true)
    {
        $rdata = new CompanyFile;
        if ($type == 'regulatory') {
            $rdata = $rdata->with('typeinfo');
            $visible = ['Id', 'Name', 'State', 'TypeId', 'CreateTime','typeinfo'=>['Name']];
        }else{
            return moerr('缺少参数');
        }
        if (!empty($data['field'])) {
            $visible = array_merge($data['field'], $visible);
        }

        $option = come_model_option($data, $rdata, $is_page);

        $rdata = $option['rdata'];
        $data  = $option['data'];

        if (!empty($data['field'])) {
            $rdata = $rdata->field($visible);
        }
        if (!empty($visible)) {
            $rdata = $rdata->visible($visible);
        }
        if (!$is_page) {
            $rdata = $rdata->limit(10);
        }
        $rdata = $rdata->select();

        $ndata['list'] = $rdata->toArray();

        if ($is_page) {
            $ndata['pageinfo'] = ['pageindex' => $data['pageindex'], 'pagemax' => $data['count']];
        }
        return $ndata;
    }
    
     /**
     * 获取信息
     * @param  [type] $id [description]
     * @return [type]     [description]
     */
    public static function user_getinfo($id, $hidden = ['OperationLog'])
    {
        $data = CompanyFile::get($id);
        if (empty($data)) {
            return moerr('没有找到您需要的信息');
        }
        $data['power_reinfo'] = false;//修改摸排表权限
        if($data['CompanyId']==-1){
            if (Rolepower::ispower(206002)) {
                //管理员权限
                $data['power_reinfo'] = true;
            }else{
                return moerr('权限不足，无法查看此客户档案信息');  
            }
            $company['Name'] = '【本企业】';
        }else{
            //权限验证
            $company = Company::get($data['CompanyId']);
            if ($data['CreateUserId'] == Pub::userid()) {
                //创建者 通过验证
                $data['power_reinfo'] = true;
            } else if (Rolepower::ispower(204007)) {
                //管理员权限
                $data['power_reinfo'] = true;
            } else if ($company->BussinessId == Pub::userid()) {
                //客户负责人 通过验证
                $data['power_reinfo'] = true;
            } else {
                //判断是否为客户
                $mycompany = Company::where('UserId',Pub::userid())->find();
                if(!empty($mycompany) && $company['CompanyId']==$mycompany['CompanyId']){
                    //企业负责人通过验证
                }else{
                    return moerr('权限不足，无法查看此客户档案信息');
                }
            }
        }
        $data['CompanyName']     = $company['Name'];
        $data['Subscribe'] = Subscribe::where('CompanyFileId',$id)->find();
        return $data;
    }

    /**
     * 修改企业文件
     * @param  [type] $data [description]
     * @return [type]       [description]
     */
    public static function user_re($idata)
    {
        if (empty($idata['FileId'])) {
            return moerr('缺少企业文件id');
        }
        $Customer = CompanyFile::get($idata['FileId']);
        if (empty($Customer)) {
            return moerr('没找到文件信息');
        }
        if($Customer['CompanyId']=='-1'){
            if (Rolepower::ispower(206002)) {
                //管理员权限
            }else{
                return moerr('权限不足，无法查看客户文件信息','','goback');
            }
        }else{
            //权限验证
            $company = Company::get($Customer['CompanyId']);
            if ($Customer['CreateUserId'] == Pub::userid()) {
                //创建排查清单 通过验证
            } else if (Rolepower::ispower(204007)) {
                //管理员权限
            } else if ($company->BussinessId == Pub::userid()) {
                //客户负责人 通过验证
            } else {
                return moerr('权限不足，无法修改排查信息');
            }
        }
        $Customer->CreateUserId = Pub::userid();
        $filed                  = ['Img', 'Name', 'Note', 'TypeId', 'SubscribeTime','File','CompanyId','SubscribeTime'];
        foreach ($filed as $key => $value) {
            if (isset($idata[$value])) {
                $Customer->$value = $idata[$value];
            }
        }
        $Customer->save();
        return $Customer->Id;
    }

}
